var express = require('express');
var router = express.Router();
const QuestModel = require("../db/questModel");
const AnswerModel = require("../db/answerModel");

module.exports = async (req, res) => {
    const id = req.params["id"]
    QuestModel.find({"id": parseInt(id), "username": req.user.username}, {
        "_id": 0,
        "id": 1,
        "title": 1,
        "description": 1,
        "lastEditedTime": 1,
        "releaseTime": 1,
        "endTime": 1,
        "questions": 1,
    }, async function (error, data) {
        if (error) {
            data[0]._doc["code"] = 0
            res.send(data[0]._doc)
        } else {
            if (data[0] === undefined) {
                res.send({
                    "code": 1
                })
            }
            const temp = await AnswerModel.find({"questId": parseInt(id)}).count()
            data[0]._doc["answerCount"] = temp
            data[0]._doc["code"] = 1
            AnswerModel.find({"questId": parseInt(id)}, function (error, ret) {
                for (let i = 0; i < data[0]._doc["questions"].length; i++)//遍历问题
                    if (data[0]._doc["questions"][i]["type"] !== 2) {
                        for (let j = 0; j < data[0]._doc["questions"][i]["option"].length; j++) {//遍历选项
                            var obj = {"name": data[0]._doc["questions"][i]["option"][j]["text"], "value": 0}

                            if (data[0]._doc["questions"][i]["type"] === 0) {
                                for (let k = 0; k < ret.length; k++) {
                                    if(ret[k]._doc["answers"][i]["answer"] !== null)
                                        if (ret[k]._doc["answers"][i]["answer"] === j + 1)
                                            obj["value"]++
                                }
                            } else {

                                for (let k = 0; k < ret.length; k++) {
                                    if(ret[k]._doc["answers"][i]["answer"] !== null)
                                    for (let l = 0; l < ret[k]._doc["answers"][i]["answer"].length; l++)
                                        if (ret[k]._doc["answers"][i]["answer"][l] === j + 1)
                                            obj["value"]++
                                }
                            }
                            data[0]._doc["questions"][i]["option"][j] = obj
                        }
                    } else {
                        var ar = new Array()
                        for (let k = 0,p = 0; p < ret.length; k++,p++) {
                            if(ret[p]._doc["answers"][i]["answer"] !== null)
                                ar[k] = ret[p]._doc["answers"][i]["answer"]
                            else
                                k--
                        }
                        data[0]._doc["questions"][i]["no"] = i + 1
                        data[0]._doc["questions"][i]["option"] = ar
                    }
                for (let i = 0; i < data[0]._doc["questions"].length; i++)
                    if (data[0]._doc["questions"][i] === false)
                        data[0]._doc["questions"].splice(i, 1)
                res.send(data[0]._doc)
            })
        }
    })
};
